package de.vocabulary.client.editor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gwt.advanced.client.datamodel.ComboBoxDataModel;
import org.gwt.advanced.client.ui.widget.ComboBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.Widget;

import de.vocabulary.client.Dialog;
import de.vocabulary.client.service.GetTranslationsCallback;
import de.vocabulary.client.service.MyVocabularyCallback;
import de.vocabulary.client.service.VocabularyService;
import de.vocabulary.client.service.VocabularyServiceAsync;
import de.vocabulary.client.service.VoidCallback;
import de.vocabulary.client.LanguageModuleManager;
import de.vocabulary.shared.model.LanguageModule;

public class AddVocabulary extends Composite  {

	private static EnterVocabularyUiBinder uiBinder = GWT
			.create(EnterVocabularyUiBinder.class);

	interface EnterVocabularyUiBinder extends UiBinder<Widget, AddVocabulary> {
	}

	private VocabularyServiceAsync service;

	@UiField VocableTable table;
	@UiField Label label_verticalFirst;
	@UiField Button button_addVocabulary;
	@UiField Button button_save;
	@UiField Button button_getTranslations;
	@UiField SimpleCheckBox checkbox_verticalFirst;
	@UiField ComboBoxWidget lesson;

	//@TODO needs to be loaded
	private LanguageModule languageModule;
	
	ComboBoxDataModel lessons = new ComboBoxDataModel();

	protected void onLoad(){
		
		languageModule = LanguageModuleManager.getLanguageModuleManager().getLanguageModule();
		
		//initialize with initial vocabulary
		service = 
				(VocabularyServiceAsync) GWT.create(VocabularyService.class);
		ServiceDefTarget serviceDef = (ServiceDefTarget) service;
		serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
				+ "vocabularyService");

		/*MyVocabularyCallback myUserCallback = new MyVocabularyCallback(table, true);
		service.getVocabularyList(myUserCallback);*/

		//initialize label
		label_verticalFirst.setText("Enter "+languageModule.getSourceLanguage()+" column first (on enter)");
		checkbox_verticalFirst.setValue(languageModule.isVerticalEditMode());
		
		table.addNewVocables(5);
		
		lesson.getComboBox().setModel(lessons);
		service.getLessons(new GetLessonsCallback());
		
		//lesson.setCustomTextAllowed(true);
		
		
		/*ComboBox<ComboBoxDataModel> language2Word = new ComboBox<ComboBoxDataModel>();
		 ComboBoxDataModel model = new ComboBoxDataModel();
		 Map<String, Object> m = new HashMap<String, Object>();
		 m.put("test", "Test");
		 m.put("testrrr", "Testdsf");
		 model.add(m);
		 model.setSelectedIndex(0);
		 language2Word.setModel(model);
		 language2Word.setCustomTextAllowed(true);
		 language2Word.setLazyRenderingEnabled(false);*/
	
	}
	public AddVocabulary() {
		//table = new MyTable();
		initWidget(uiBinder.createAndBindUi(this));


	}




	@UiHandler("button_addVocabulary")
	void addVocabularyEventhandler(ClickEvent e) {
		table.addNewVocables(4);
	}

	@UiHandler("button_save")
	void saveEventhandler(ClickEvent event) {
		VoidCallback myUserCallback = new VoidCallback();
		service.storeVocabularyList(table.getStoredVocabulary(lessons.getSelectedId()), myUserCallback);
	}

	@UiHandler("button_getTranslations")
	void getTranslationsEventhandler(ClickEvent event) {
		service.getTranslations(table.getStoredVocabulary(lessons.getSelectedId()), new GetTranslationsCallback());
	}
	
	public class GetTranslationsCallback implements AsyncCallback<List<List<String>>> {

		@Override
		public void onFailure(Throwable caught) {
			 Window.alert("GetTranslations: "+caught.getMessage());
			
		}
		
		
		@Override
		public void onSuccess(List<List<String>> result) {
			table.setTranslations(result);
			
		}

	}
	
	public class GetLessonsCallback implements AsyncCallback<List<String>> {

		@Override
		public void onFailure(Throwable caught) {
			 Window.alert(caught.getMessage());
			
		}
		
		
		@Override
		public void onSuccess(List<String> result) {
			lessons.clear();
			for (String lesson: result){
				lessons.add(lesson, lesson);
			}
			
		}

	}

	@UiHandler("checkbox_verticalFirst")
	void updateVerticalEditing(ClickEvent event){
		table.updateTabIndex(checkbox_verticalFirst.getValue());
	}





}
